<template>
  <div class="table-box">
    <ProTable ref="proRef" :request-api="list" :columns="columns">
      <template #tableHeader="scope">
        <el-button type="primary" @click="opera(undefined)">创建友情链接</el-button>
        <el-button type="danger" @click="deleteRow(scope.selectedListIds)" :disabled="scope.selectedListIds.length === 0"
          >批量删除</el-button
        >
      </template>
      <template #opera="{ row }">
        <el-button type="primary" link :icon="EditPen" @click="opera(row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteRow([row.id])">删除</el-button>
      </template>
    </ProTable>
    <friendlyDrawer ref="freRef"></friendlyDrawer>
  </div>
</template>

<script setup lang="ts">
import ProTable from "@/components/ProTable/index.vue";
import { ref } from "vue";
import { list, createApi, editApi, deleteApi } from "@/api/modules/friendlyLinks";
import { ColumnProps } from "@/components/ProTable/interface";
import { EditPen, Delete } from "@element-plus/icons-vue";
import friendlyDrawer from "./components/friendlyDrawer.vue";
import { useHandleData } from "@/hooks/useHandleData";

const freRef = ref<InstanceType<typeof friendlyDrawer> | null>();

const proRef = ref<InstanceType<typeof ProTable> | null>();

const columns: ColumnProps[] = [
  { type: "selection", width: 80 },
  { prop: "sort", label: "排序值" },
  { prop: "title", label: "连接名称" },
  { prop: "link", label: "链接地址" },
  { prop: "opera", label: "操作" }
];

const opera = (row: any) => {
  const params: any = {
    row: row ? row : { title: "", link: "", sort: 1 },
    api: row ? editApi : createApi,
    getTable: proRef.value?.getTableList
  };
  freRef.value?.acceptParams(params);
};

// 删除
const deleteRow = async (ids: any) => {
  try {
    await useHandleData(deleteApi, { ids }, "删除所选内容");
    proRef.value?.getTableList!();
  } catch (error) {}
};
</script>

<style></style>
